# Copyright 1999-2007 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

EAPI="1"
inherit eutils
MY_PN="${PN/handbrake/HandBrake}"
MY_P="${P/handbrake/HandBrake}"

DESCRIPTION="Open-source DVD to MPEG-4 converter."
HOMEPAGE="http://handbrake.m0k.org/"
SLOT="0"
LICENSE="GPL-2"

RESTRICT="mirror"     # Remove me before release!

# Too bad this doesn't work :(
# SRC_URI="http://saintdevelopment.com/mirror/handbrake/${MY_P}.tar.gz
# http://download.m0k.org/handbrake/contrib/a52dec-0.7.4.tar.gz
# http://download.m0k.org/handbrake/contrib/faac-1.24.tar.gz
# http://download.m0k.org/handbrake/contrib/ffmpeg-9816.tar.gz
# http://download.m0k.org/handbrake/contrib/lame-3.96.1.tar.gz
# http://download.m0k.org/handbrake/contrib/libdca-r81-strapped.tar.gz
# http://download.m0k.org/handbrake/contrib/libdvdcss-1.2.9.tar.gz
# http://download.m0k.org/handbrake/contrib/libdvdread-0.9.7.tar.gz
# http://download.m0k.org/handbrake/contrib/libmkv-0.6.1.2.tar.gz
# http://download.m0k.org/handbrake/contrib/libogg-1.1.2.tar.gz
# http://download.m0k.org/handbrake/contrib/libsamplerate-0.1.2.tar.gz
# http://download.m0k.org/handbrake/contrib/libvorbis-aotuv_b5.tar.gz
# http://download.m0k.org/handbrake/contrib/mpeg2dec-0.4.1.tar.gz
# http://download.m0k.org/handbrake/contrib/mpeg4ip-1.3.tar.gz
# http://download.m0k.org/handbrake/contrib/x264-r665.tar.gz 
# http://download.m0k.org/handbrake/contrib/xvidcore-1.1.2.tar.gz"

SRC_URI="mirror://gentoo/HandBrake-complete-0.9.1.tar.bz2"

KEYWORDS="~x86 ~amd64"

IUSE="-sys-libs +aotuv"

RDEPEND="sys-libs/zlib
		sys-libs? ( >=media-video/ffmpeg-0.4.9_p20070616-r1
					>=media-libs/libogg-1.1.2
					>=media-libs/faac-1.24
					>=media-libs/xvid-1.1.2
					>=media-sound/lame-3.96.1
					>=media-libs/libdvdcss-1.2.9
					>=media-libs/libsamplerate-0.1.2
					>=media-libs/libdvdread-0.9.7
					!aotuv? ( >=media-libs/libvorbis-1.1.1 )
				)"
#		ruby? ( dev-lang/ruby )"

DEPEND="dev-util/jam
		${RDEPEND}"

S="${WORKDIR}/${MY_PN}"

CONTRIB="${S}/contrib"

pkg_setup() {
	if use sys-libs; then
		ewarn "You are electing to use system libraries instead of the ones"
		ewarn "provided by upstream. If anything breaks file a bug at "
		ewarn "bugs.gentoo.org. Also note, some system libraries are not used"
		ewarn "as this would break some major functionality."
		ewarn "YOU HAVE BEEN WARNED!"
		epause
	fi
}

src_unpack() {
	PACKAGES="a52dec libdca"
	unpack ${A}

	cd "${S}"
	epatch "${FILESDIR}/${P}-Jamfile-nocontrib.patch"
	if use sys-libs ; then
		if use aotuv ; then
			epatch "${FILESDIR}/${P}-system-libs.patch"
		else
			epatch "${FILESDIR}/${P}-system-libs-noaotuv.patch"
		fi
	else
		PACKAGES="${PACKAGES} ffmpeg"
	fi

	for subpackage in ${PACKAGES} ; do
		cd "${WORKDIR}/${subpackage}"
		epatch "${CONTRIB}/patch-${subpackage}.patch"
	done

	cd "${WORKDIR}/mpeg4ip"
	epatch "${CONTRIB}/patch-mpeg4ip.patch"
	epatch "${CONTRIB}/patch-mpeg4ip-pascal-str.patch"

	cd "${WORKDIR}/mpeg2dec"
	epatch "${CONTRIB}/patch-libmpeg2.patch"

	cd "${WORKDIR}/x264"
	epatch "${CONTRIB}/patch-x264-aq.patch"
	epatch "${CONTRIB}/patch-x264-idr.patch"
}

src_compile() {
	# Stolen from the dev-util/boost ebuild, thanks a lot!
	NUMJOBS=$(sed -e 's/.*\(\-j[ 0-9]\+\) .*/\1/' <<< ${MAKEOPTS})

	PACKAGES="a52dec libdca libmkv"
	if ! use sys-libs ; then
		PACKAGES="${PACKAGES} libdvdcss faac lame libogg libsamplerate"
	elif use aotuv ; then
		PACKAGES="${PACKAGES} libvorbis"
	fi

# Contribs go first.
# Configure everything first
	for subpackage in ${PACKAGES} ; do
		einfo "Configuring ${subpackage}."
		cd "${WORKDIR}/${subpackage}"
		./configure --host=${CHOST} \
					--prefix=${CONTRIB} \
					--disable-shared \
					--enable-static \
					|| die "configure ${subpackage} failed"
	done

	if ! use sys-libs ; then
		einfo "Configuring ffmpeg."
		cd "${WORKDIR}/ffmpeg"
		./configure --prefix=${CONTRIB} \
					--enable-gpl \
					--enable-pthreads \
					--enable-swscaler \
					--disable-audio-beos \
					--disable-shared \
					--enable-static \
					--disable-decoders \
					--enable-decoder=mp2 \
					--disable-parsers \
					--enable-parser=mpegaudio \
					--disable-encoders \
					--enable-encoder=mpeg4 \
					--enable-encoder=snow \
					--disable-muxers \
					--disable-demuxers \
					--disable-protocols \
					--disable-bsfs \
					|| die "configure ffmpeg failed"

		einfo "Configuring xvidcore."
		cd "${WORKDIR}/xvidcore/build/generic"
		./configure --prefix=${CONTRIB} || die "configure xvidcore failed"

		einfo "Configuring libvorbis."
		cd "${WORKDIR}/libvorbis"
		./configure --prefix=${CONTRIB} \
					--with-ogg=${CONTRIB} \
					--disable-shared \
					|| die "configure libvorbis failed"

		einfo "Configuring libdvdread."
		cd "${WORKDIR}/libdvdread"
		./configure --prefix=${CONTRIB} \
					--disable-shared \
					--enable-static \
					--with-libdvdcss=${CONTRIB} \
					|| die "configure libdvdread failed"
	fi

	einfo "Configuring libmp4v2."
	cd "${WORKDIR}/mpeg4ip"
	./bootstrap --disable-mp3lame \
				--disable-faac \
				--disable-x264 \
				--disable-server \
				--disable-player \
				|| die "configure libmp4v2 failed"

	einfo "Configuring libmpeg2."
	cd "${WORKDIR}/mpeg2dec"
	./configure --prefix=${CONTRIB} \
				--disable-shared \
				--disable-sdl \
				--enable-static \
				|| die "configure libmpeg2 failed"

	einfo "Configuring x264."
	cd "${WORKDIR}/x264"
	./configure --prefix=${CONTRIB} \
				--enable-pthread \
				|| die "configure x264 failed"

# Make and install all the contribs.
	PACKAGES="a52dec libdca libmkv mpeg2dec x264"
	if ! use sys-libs ; then
		PACKAGES="${PACKAGES} ffmpeg libdvdcss libdvdread faac lame libogg libsamplerate libvorbis xvidcore/build/generic"
	elif use aotuv ; then
		PACKAGES="${PACKAGES} libvorbis"
	fi

	for subpackage in ${PACKAGES} ; do
		einfo "Building ${subpackage}."
		cd "${WORKDIR}/${subpackage}"
		emake || die "emake ${subpackage} failed"
		einfo "Installing ${subpackage} into contrib dir."
		emake install || die "install ${subpackage} failed"
	done

	einfo "Building libmp4v2."
	cd "${WORKDIR}/mpeg4ip"
	make ${MAKEOPTS} -C lib/mp4v2 libmp4v2.la
	einfo "Installing libmp4v2 into contrib dir."
	cp lib/mp4v2/.libs/libmp4v2.a ${CONTRIB}/lib
	cp mpeg4ip_config.h include/mpeg4ip.h include/mpeg4ip_version.h \
			include/mpeg4ip_win32.h lib/mp4v2/*.h ${CONTRIB}/include

	# Probably shouldn't call strip directly.
	strip -S "${CONTRIB}/lib/*.a"

# Build HandBrake itself now.
	einfo "Configuring HandBrake."
	cd "${S}"
	./configure || die "configure HandBrake failed"
	einfo "Building HandBrake."
	jam -q ${NUMJOBS} || die "jam HandBrake failed"
}

src_install() {
	# Not really much to install :)
	into /usr
	dobin HandBrakeCLI

	dodoc AUTHORS BUILD CREDITS NEWS THANKS TRANSLATIONS
#	if use ruby ; then
#		insinto /usr/share/doc/${PF}/scripts
#		doins scripts/*.rb
#	fi
}
